package com.wuxiaofan.api;

import com.alibaba.fastjson.JSONObject;
import com.wuxiaofan.vo.MessageInfo;
import com.wuxiaofan.vo.ReturnResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ArrayBlockingQueue;

@RestController
public class TestApi {


    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("test1")
    public ReturnResult test1(){
        return ReturnResult.success("测试成功,用户服务");
    }

    @RequestMapping("test2")
    public ReturnResult test2() throws Exception{
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setMessageContext("sfasdfas");
        messageInfo.setMessageId("1");
        rocketMQTemplate.convertAndSend("defaultProducer","sdfasfasdf");
        return ReturnResult.success("测试成功,用户服务");
    }


    @RequestMapping("test3")
    public ReturnResult test3() throws Exception{
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setMessageContext("sfasdfas");
        messageInfo.setMessageId("1");
        Message message = new Message() {
            @Override
            public Object getPayload() {
                return messageInfo;
            }

            @Override
            public MessageHeaders getHeaders() {
                return null;
            }
        };
        rocketMQTemplate.sendMessageInTransaction("defaultProducer",message,null);
        return ReturnResult.success("测试成功,用户服务");
    }


}
